//
//  YYECourseCollectionViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/4.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYECourseCollectionViewController.h"
#import "YYECourseCollectionTableViewCell.h"
#import "YYECourseCollModel.h"
#import "YYECoursePlayViewController.h"
#import "YYECourseNoBuyViewController.h"


@interface YYECourseCollectionViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) YYEBaseTableView *tableView;
@property (nonatomic,strong) NSMutableArray *datas;

@end

@implementation YYECourseCollectionViewController

static NSString *collectionCell = @"collectionCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"课程收藏";
    
    [self configUI];
    
    [self loadData];
    
}

- (void)loadData {
    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token
                          };
    [SVProgressHUD show];
    [AFNHelper post:CourseCollShow parameters:dic success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            self.datas = [YYECourseCollModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.tableView reloadData];
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD showWithStatus:ShowError];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }];
    
}

- (void)configUI {
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[YYECourseCollectionTableViewCell class] forCellReuseIdentifier:collectionCell];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
    
}

- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}

- (NSMutableArray *)datas {
    
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
    
}

#pragma mark -- UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.datas.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYECourseCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:collectionCell];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.datas[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return WSCALE * 60 + 20;
    
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewRowAction *cellAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"取消\n收藏" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"dianji l ");
    }];
    
    cellAction.backgroundColor = GeneralRedColor;
    
    return @[cellAction];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    YYECourseCollModel *model = self.datas[indexPath.row];
    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          @"cid":model.cid
                          };
    [SVProgressHUD show];
    [AFNHelper post:LessonAuthority parameters:dic success:^(id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            
            
            NSString *isBuy = responseObject[@"data"][@"is_buy"];
            NSString *isCollect = [responseObject[@"data"][@"is_collect"] stringValue];
            NSString *mid = responseObject[@"data"][@"mid"];
            NSString *playback = responseObject[@"data"][@"playback"];
            
            if (isBuy.integerValue == 1) {
                YYECoursePlayViewController *vc = [[YYECoursePlayViewController alloc]init];
                vc.cid = model.cid;
                vc.isBuy = isBuy;
                vc.isCollect = isCollect;
                vc.mid = mid;
                vc.playback = playback;
                [self.navigationController pushViewController:vc animated:YES];
            }
            else {
                
                YYECourseNoBuyViewController *vc = [[YYECourseNoBuyViewController alloc]init];
                vc.cid = model.cid;
                vc.img = model.img;
                vc.isBuy = isBuy;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
            
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
